package com.wx.tnsperson.service.impl;


import com.wx.tnsperson.mapper.ProResumeMapper;
import com.wx.tnsperson.model.ProResumeModel;
import com.wx.tnsperson.service.ProResumeService;
import com.wx.tnsperson.vo.ResumeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * @Classname ProResumeServiceImpl
 * @Description TODO
 * @Date 2020/6/24 10:15
 * @Created by llh
 */
@Slf4j
@Service
public class ProResumeServiceImpl implements ProResumeService {
    @Autowired
    private ProResumeMapper proResumeMapper;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat ( "yyyy/MM/dd/" );

    @Value("${file-save-path}")
    private String fileSavePath;


    @Override
    public List<ResumeVo> findAll () {
        return proResumeMapper.findAll ( );
    }

    @Override
    public ResumeVo findById ( Integer id ) {
        return proResumeMapper.findById ( id );
    }

    @Override
    public Integer delResume ( Integer id ) {
        return proResumeMapper.delResume ( id );
    }

    //重新封装ResumeVo返回给前端 将图片存储在ResumeVo对象中
    @Override
    public Integer updateFile ( ProResumeModel proResumeModel,MultipartFile headPh,HttpServletRequest request ) {
        String picPath = saveHrPic ( headPh,request );
        ResumeVo resumeVo = new ResumeVo ( );
        BeanUtils.copyProperties ( proResumeModel,resumeVo );
        resumeVo.setHeadPh ( picPath );
        return proResumeMapper.updateFile ( resumeVo );
    }


    @Override
    public Integer updateResume ( ResumeVo resumeVo ) {
        return proResumeMapper.updateResume ( resumeVo );
    }

    //保存头像
    private String saveHrPic ( MultipartFile headPh,HttpServletRequest request ) {
        //保存数据库的路径
        String sqlPath = null;
        String directory = simpleDateFormat.format ( new Date ( ) );
        //文件不存在就创建文件目录
        File file = new File ( fileSavePath + directory );
        if (!file.exists ( )) {
            file.mkdirs ( );
        }
        //获取文件后缀
        String substring = headPh.getOriginalFilename ( ).substring ( headPh.getOriginalFilename ( ).lastIndexOf ( "." ) );
        //新文件名
        String filename = UUID.randomUUID ( ).toString ( ).replaceAll ( "-","" ) + substring;
        File newFile = new File ( fileSavePath + directory + filename );
        //保存文件
        try {
            headPh.transferTo ( newFile );
        } catch (IOException e) {
            e.printStackTrace ( );
        }
        //返回文件路径
        sqlPath = request.getScheme ( ) + "://" + request.getServerName () + ":" + request.getServerPort ( ) + "/images/" + directory + filename;
        System.out.println (sqlPath );
        return sqlPath;

    }

}
